package com.ytkj.tradeserver.controller.articleInfo;

import com.ytkj.tradeserver.constant.ComConstants;
import com.ytkj.tradeserver.entity.Articleinfo;
import com.ytkj.tradeserver.entity.AttachmentFileVO;
import com.ytkj.tradeserver.entity.LookupClassifyVO;
import com.ytkj.tradeserver.entity.UserInfo;
import com.ytkj.tradeserver.model.PageBean;
import com.ytkj.tradeserver.service.iservice.IArticleinfoService;
import com.ytkj.tradeserver.service.iservice.IAttachmentFileService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/9.
 */
@Controller
@RequestMapping("/articleInfo")
public class ArticleInfoController {

    @Resource
    IArticleinfoService articleinfoService;

    @Resource
    private IAttachmentFileService fileService;

    //跳转至新增
    @RequestMapping("/returnAddArticleInfo")
    public String goPushMessage(){
        return "articleInfo/addArticleInfo";
    }

    @RequestMapping(value = "/findAllArticles",method = RequestMethod.GET)
    public ModelAndView queryAllArticles(@RequestParam(value="pageNum",required = false,defaultValue= ComConstants.PAGENUM) int pageNum,
                                         @RequestParam(value="pageSize",required = false,defaultValue=ComConstants.PAGESIZE) int pageSize,
                                         Articleinfo info, HttpServletRequest request){
        String url = request.getContextPath() + request.getServletPath();
        PageBean<Articleinfo> messageList = articleinfoService.queryAllArticles(url,info,new PageBean(pageNum,pageSize));
        return new ModelAndView("articleInfo/articleList","pageBean",messageList);
    }

    @RequestMapping(value="/saveArticleInfo",method = RequestMethod.POST)
    public String addArticleInfo(@RequestParam("articleSmallPics") CommonsMultipartFile articleSmallPics[],
                                 Articleinfo info, HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession(true).getAttribute("userInfo");
        info.setCreator(userInfo.getId());
        info.setLastUpdateUser(userInfo.getId());
        articleinfoService.addArticle(info);
        fileService.saveMultiUploadFile(String.valueOf(info.getId()),ComConstants.FILETYPE.ArticleSmallPic.getValue(),userInfo,articleSmallPics,request);
        return  "redirect:/articleInfo/findAllArticles";
    }


    @RequestMapping(value="/queryNewDetail/{id}", method = RequestMethod.GET)
    public ModelAndView querySingleArticle(@PathVariable(value="id") Integer id) {
        Articleinfo articleinfo = articleinfoService.querySingleArticle(new Articleinfo(id));
        if (articleinfo==null){
            return new ModelAndView("errorpage/404","errorMsg","没有找到文章信息");
        }
        List<LookupClassifyVO> types = new ArrayList<LookupClassifyVO>();
        LookupClassifyVO one = new LookupClassifyVO("01","科技");types.add(one);
        LookupClassifyVO two = new LookupClassifyVO("02","经济");types.add(two);
        LookupClassifyVO three = new LookupClassifyVO("03","生物");types.add(three);
        LookupClassifyVO four = new LookupClassifyVO("04","文学");types.add(four);
        LookupClassifyVO five = new LookupClassifyVO("05","军事");types.add(five);
        LookupClassifyVO six = new LookupClassifyVO("06","历史");types.add(six);
        articleinfo.setTypeLookups(types);
        List<LookupClassifyVO> appTypes = new ArrayList<LookupClassifyVO>();
        LookupClassifyVO atone = new LookupClassifyVO("01","Article");appTypes.add(atone);
        articleinfo.setAppTypeLookups(appTypes);
        List<LookupClassifyVO> setTypes = new ArrayList<LookupClassifyVO>();
        LookupClassifyVO setTypeOne = new LookupClassifyVO("1","文上图下");setTypes.add(setTypeOne);
        LookupClassifyVO setTypeTwo = new LookupClassifyVO("2","文左图右");setTypes.add(setTypeTwo);
        LookupClassifyVO setTypeThree = new LookupClassifyVO("3","文左图右");setTypes.add(setTypeThree);
        articleinfo.setSetTypeLookups(setTypes);
        AttachmentFileVO file = new AttachmentFileVO(ComConstants.FILETYPE.ArticleSmallPic.getValue(),String.valueOf(articleinfo.getId()));
        articleinfo.setPictures(fileService.queryFiles(file));
        return new ModelAndView("articleInfo/editArticleInfo").addObject("articleInfo",articleinfo);
    }
}
